package com.ctg.itrdc.k8sconsole.service.impl;

import java.net.URL;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.HttpRequestUtil;
import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.dto.K8sNodeDTO;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.k8sconsole.service.IK8snodeService;
import com.ctg.itrdc.paspsdk.common.utils.file.PropertiesUtil;

/**
 * 
 * @description:
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年8月20日 上午10:39:14 Piao.Wu v1.0.0 create
 *
 */
@SuppressWarnings("static-access")
@Component("k8snodeService")
public class K8snodeServiceImpl implements IK8snodeService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IClusterConfigDao clusterConfigDao;

	
	@Override
	public String getNodes(String clusterCode, String perpage, String page, String nodeip, String groupId)
			throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getNodes.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode);
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = requestUtil.target;
		Response response = null;
		if (StringUtils.isNotEmpty(groupId)) {
			response = target.queryParam("perpage", perpage).queryParam("page", page).queryParam("name", nodeip)
					.queryParam("groupId", groupId).request().get();
		} else {
			response = target.queryParam("perpage", perpage).queryParam("page", page).queryParam("name", nodeip)
					.request().get();
		}

		logger.info("========getNodes.api返回的状态码:" + response.getStatus());
		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getNodesByIps(String clusterCode, String ips) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("ips", ips);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getNodesByIps.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode);
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = requestUtil.target;
		Response response = target.request().post(Entity.entity(paraMap, MediaType.APPLICATION_JSON_TYPE));

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getNodeDetail(String clusterCode, String nodeip) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getNodeDetail.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, nodeip);
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = requestUtil.target;
		Response response = target.request().get();

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getNodeResource(K8sNodeDTO k8sNodeDTO) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(k8sNodeDTO.getClusterCode());

		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("nodeName", k8sNodeDTO.getNodeips());
		
		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getNodeResource.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), k8sNodeDTO.getClusterCode());
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = requestUtil.target;
		Response response = target.request().post(Entity.entity(paraMap, MediaType.APPLICATION_JSON_TYPE));

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getClusterResrTotal(String clusterCode) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getClusterResrTotal.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode);
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = requestUtil.target;
		Response response = target.request().get();

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String startOrShutdownNode(String clusterCode, String nodeip, Boolean unschedulable) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("unschedulable", unschedulable);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "startOrShutdownNode.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, nodeip);
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = requestUtil.target;
		Response response = target.request().post(Entity.entity(paraMap, MediaType.APPLICATION_JSON_TYPE));

		return requestUtil.getResponseJsonData(response);
	}

}
